package com.cqq.print.business.service.impl;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cqq.common.core.bean.PageInfoBean;
import com.cqq.print.business.dao.POrderDao;
import com.cqq.print.business.dao.POrderDetailDao;
import com.cqq.print.business.dao.domain.POrder;
import com.cqq.print.business.dao.domain.POrderDetail;
import com.cqq.print.business.dao.query.POrderQuery;
import com.cqq.print.business.service.POrderService;
import com.cqq.print.business.service.bean.POrderBean;
import com.cqq.print.business.service.bean.POrderDetailBean;
import com.cqq.print.business.service.bean.POrderQueryBean;


/**
 * @author generated
 * @date 2015-07-01
 */


@Service(value="pOrderService")
public class POrderServiceImpl implements POrderService{

             @Autowired
             private POrderDao pOrderDao;
             @Autowired
             private POrderDetailDao pOrderDetailDao;
             
			
			public int insertSelective(POrderBean pOrderBean) {
				if(pOrderBean==null)
					return 0;
				POrder pOrder = new POrder();
				BeanUtils.copyProperties(pOrderBean, pOrder);
				return pOrderDao.insertSelective(pOrder);
			}

			
			public PageInfoBean<POrderBean> queryPageOrders(
					Map<String, Object> map) {
				List<POrderBean> listPOBean = null;
				List<POrder> listPO = pOrderDao.queryOrders(map);
				int count = pOrderDao.queryOrdersCount(map);
				if(listPO!=null && listPO.size()>0){
					listPOBean = new ArrayList<POrderBean>();
					for (POrder po : listPO) {
						POrderBean poBean = new POrderBean();
						BeanUtils.copyProperties(po, poBean);
						listPOBean.add(poBean);
					}
				}
				
				return new PageInfoBean<POrderBean>(listPOBean, count);
			}

			
			public POrderBean queryOrderAndDetailById(int id) {
				POrder po = pOrderDao.queryOrderById(id); 
				List<POrderDetail> listDetail = pOrderDetailDao.queryDetailsByOrderId(id);
				POrderBean poBean = new POrderBean();
				BeanUtils.copyProperties(po, poBean);
				
				List<POrderDetailBean> listDetailBean = null;
				if(listDetail!=null && listDetail.size()>0){
					listDetailBean = new ArrayList<POrderDetailBean>();
					for (POrderDetail poD : listDetail) {
						POrderDetailBean poDBean = new POrderDetailBean();
						BeanUtils.copyProperties(poD, poDBean);
						listDetailBean.add(poDBean);
					}
				}
				poBean.setListDetail(listDetailBean);
				return poBean;
			}

			
			public POrderBean queryOrderByorderNum(String orderNum) {
				POrder po = pOrderDao.queryOrderByorderNum(orderNum);
				POrderBean poBean = new POrderBean();
				BeanUtils.copyProperties(po, poBean);
				return poBean;
			}

			
			public Map<String,Object> getMaxOrderNumToDay(String currDate) {
				return pOrderDao.getMaxOrderNumToDay(currDate);
			}

			
			public POrderBean queryOrderById(int id) {
				POrder po = pOrderDao.queryOrderById(id); 
				POrderBean poBean = new POrderBean();
				BeanUtils.copyProperties(po, poBean);
				return poBean;
			}

			
			public int updateOrder(POrderBean pOrderBean) {
				if(pOrderBean==null||pOrderBean.getId()==null)
					return 0;
				POrder pOrder = new POrder();
				BeanUtils.copyProperties(pOrderBean, pOrder);
				return pOrderDao.updateByPrimaryKeySelective(pOrder);
			}

			
			public int updateCheckStatus(POrderBean pOrderBean) {
				if(pOrderBean==null||pOrderBean.getId()==null||pOrderBean.getCheckStatus()==null)
					return 0;
				POrder pOrder = new POrder();
				BeanUtils.copyProperties(pOrderBean, pOrder);
				return pOrderDao.updateByPrimaryKeySelective(pOrder);
			}

			
			public int updateFlowStatus(POrderBean pOrderBean) {
				if(pOrderBean==null||pOrderBean.getId()==null||pOrderBean.getFlowStatus()==null)
					return 0;
				POrder pOrder = new POrder();
				BeanUtils.copyProperties(pOrderBean, pOrder);
				return pOrderDao.updateByPrimaryKeySelective(pOrder);
			}

			
			public int updateStatus(POrderBean pOrderBean) {
				if(pOrderBean==null||pOrderBean.getId()==null||pOrderBean.getStatus()==null)
					return 0;
				POrder pOrder = new POrder();
				BeanUtils.copyProperties(pOrderBean, pOrder);
				return pOrderDao.updateByPrimaryKeySelective(pOrder);
			}

			
			public PageInfoBean<POrderBean> queryOrdersForDispatch(
					POrderQueryBean queryBean) {
				POrderQuery query = new POrderQuery();
				BeanUtils.copyProperties(queryBean, query);
				List<POrderBean> listPOBean = null;
				List<POrder> listPO = pOrderDao.queryOrdersForDispatch(query);
				int count = pOrderDao.queryOrdersCountForDispatch(query);
				if(listPO!=null && listPO.size()>0){
					listPOBean = new ArrayList<POrderBean>();
					for (POrder po : listPO) {
						POrderBean poBean = new POrderBean();
						BeanUtils.copyProperties(po, poBean);
						listPOBean.add(poBean);
					}
				}
				return new PageInfoBean<POrderBean>(listPOBean, count,query.getPageSize(),query.getPageNo());
			}

}